package com.logic.landseaserver.common.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：DateBetweenUtil</br>
 * 类描述：计算两个日期相差年/月/天</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年11月13日 下午6:10:01</br>
 * 
 * @version 1.0
 *
 */
public abstract class DateBetweenUtil
{
    /**
     * 
     * [简要描述]：入口</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-11-23)</br>
     *
     * @param start
     * @param end
     * @return DateBetweenObject 计算结果
     *
     */
    public static DateBetweenObject process(Date start, Date end)
    {
        start = DateUtil.dateConvertDate(start);
        end = DateUtil.dateConvertDate(end);
        
        if (start.getTime() == end.getTime())
        {
            DateBetweenObject dateBetweenObject = new DateBetweenObject();
            List<String> dayList = new ArrayList<String>();
            SimpleDateFormat dateSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dayList.add(dateSimpleDateFormat.format(start));
            dateBetweenObject.setDayList(dayList);
            return dateBetweenObject;
        }
        
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(start);
        startDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
        startDate.set(java.util.Calendar.MINUTE, 0);
        startDate.set(java.util.Calendar.SECOND, 0);
        
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(end);
        endDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
        endDate.set(java.util.Calendar.MINUTE, 0);
        endDate.set(java.util.Calendar.SECOND, 0);
        
        DateBetweenObject dateBetweenObject = new DateBetweenObject();
        
        boolean jx = getYear(start, end, dateBetweenObject);
        if (!jx)
        {
            return dateBetweenObject;
        }
        
        jx = getMonth(dateBetweenObject.getYearEndCalendar().getTime(), end, dateBetweenObject);
        if (!jx)
        {
            return dateBetweenObject;
        }
        getDay(dateBetweenObject.getMonthEndCalendar().getTime(), end, dateBetweenObject);
        
        return dateBetweenObject;
    }
    
    private static boolean getYear(Date start, Date end, DateBetweenObject dateBetweenObject)
    {
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(start);
        startDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
        startDate.set(java.util.Calendar.MINUTE, 0);
        startDate.set(java.util.Calendar.SECOND, 0);
        
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(end);
        endDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
        endDate.set(java.util.Calendar.MINUTE, 0);
        endDate.set(java.util.Calendar.SECOND, 0);
        
        // 判断是否为同一年
        if (startDate.get(Calendar.YEAR) == endDate.get(Calendar.YEAR))
        {
            dateBetweenObject.setYearEndCalendar(startDate);
            return true;
        }
        
        List<String> yearList = dateBetweenObject.getYearList();
        
        Calendar endYearCalendar = Calendar.getInstance();
        endYearCalendar.setTime(start);
        
        SimpleDateFormat dateSimpleDateFormat = new SimpleDateFormat("yyyy");
        
        int sourceDay = startDate.get(Calendar.DAY_OF_MONTH);
        startDate.add(Calendar.YEAR, 1);
        int nowDay = startDate.get(Calendar.DAY_OF_MONTH);
        if (sourceDay == nowDay)
        {
            startDate.add(Calendar.DAY_OF_YEAR, -1);
        }
        
        System.out.println(startDate.getTime());
        int count = 0;
        while (!startDate.after(endDate))
        {
            count++;
            String year = dateSimpleDateFormat.format(startDate.getTime());
            yearList.add(year);
            
            endYearCalendar.setTime(startDate.getTime());
            
            startDate = Calendar.getInstance();
            startDate.setTime(start);
            startDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
            startDate.set(java.util.Calendar.MINUTE, 0);
            startDate.set(java.util.Calendar.SECOND, 0);
            
            sourceDay = startDate.get(Calendar.DAY_OF_MONTH);
            startDate.add(Calendar.YEAR, count + 1);
            nowDay = startDate.get(Calendar.DAY_OF_MONTH);
            
            if (sourceDay == nowDay)
            {
                startDate.add(Calendar.DAY_OF_YEAR, -1);
            }            
        }
        
        dateBetweenObject.setYearEndCalendar(endYearCalendar);
        
        if (endYearCalendar.getTimeInMillis() == endDate.getTimeInMillis())
        {
            return false;
        }
        
        // 如果跨年了，加1天
        if (dateBetweenObject.getYearList().size() > 0)
        {
            endYearCalendar.add(Calendar.DAY_OF_YEAR, 1);
            dateBetweenObject.setYearEndCalendar(endYearCalendar);
        }
        return true;
    }
    
    private static boolean getMonth(Date start, Date end, DateBetweenObject dateBetweenObject)
    {
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(start);
        startDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
        startDate.set(java.util.Calendar.MINUTE, 0);
        startDate.set(java.util.Calendar.SECOND, 0);
        
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(end);
        endDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
        endDate.set(java.util.Calendar.MINUTE, 0);
        endDate.set(java.util.Calendar.SECOND, 0);
        
        // 如果直接是同一天
        if (start.getTime() == end.getTime())
        {
            dateBetweenObject.setMonthEndCalendar(startDate);
            return true;
        }
        
        List<String> list = dateBetweenObject.getMonthList();
        
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(start);
        
        SimpleDateFormat dateSimpleDateFormat = new SimpleDateFormat("yyyy-MM");
        
        int sourceDay = startDate.get(Calendar.DAY_OF_MONTH);
        
        startDate.add(Calendar.MONTH, 1);
        
        int nowDay = startDate.get(Calendar.DAY_OF_MONTH);
        
        if (sourceDay == nowDay)
        {
            startDate.add(Calendar.DAY_OF_YEAR, -1);
        }
        
        int count = 0;
        while (!startDate.after(endDate))
        {
            count++;
            String month = dateSimpleDateFormat.format(startDate.getTime());
            list.add(month);
            endCalendar.setTime(startDate.getTime());
            
            startDate = Calendar.getInstance();
            startDate.setTime(start);
            startDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
            startDate.set(java.util.Calendar.MINUTE, 0);
            startDate.set(java.util.Calendar.SECOND, 0);
            
            sourceDay = startDate.get(Calendar.DAY_OF_MONTH);
            startDate.add(Calendar.MONTH, count + 1);
            nowDay = startDate.get(Calendar.DAY_OF_MONTH);
            
            if (sourceDay == nowDay)
            {
                startDate.add(Calendar.DAY_OF_YEAR, -1);
            }
        }
        
        dateBetweenObject.setMonthEndCalendar(endCalendar);
        
        if (endCalendar.getTimeInMillis() == endDate.getTimeInMillis())
        {
            return false;
        }
        
        // 如果跨月了，加1天
        if (dateBetweenObject.getMonthList().size() > 0)
        {
            endCalendar.add(Calendar.DAY_OF_YEAR, 1);
            dateBetweenObject.setMonthEndCalendar(endCalendar);
        }
        
        return true;
    }
    
    private static boolean getDay(Date start, Date end, DateBetweenObject dateBetweenObject)
    {
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(start);
        startDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
        startDate.set(java.util.Calendar.MINUTE, 0);
        startDate.set(java.util.Calendar.SECOND, 0);
        
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(end);
        endDate.set(java.util.Calendar.HOUR_OF_DAY, 0);
        endDate.set(java.util.Calendar.MINUTE, 0);
        endDate.set(java.util.Calendar.SECOND, 0);
        
        List<String> list = dateBetweenObject.getDayList();
        
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(start);
        
        
        SimpleDateFormat dateSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 如果直接是同一天
        if (start.getTime() == end.getTime())
        {
            dateBetweenObject.setDayEndCalendar(endCalendar);
            String day = dateSimpleDateFormat.format(startDate.getTime());
            list.add(day);
            return false;
        }
        
        startDate.add(Calendar.DAY_OF_YEAR, 1);
        while (!startDate.after(endDate))
        {
            String day = dateSimpleDateFormat.format(startDate.getTime());
            list.add(day);
            
            endCalendar.setTime(startDate.getTime());
            
            startDate.add(Calendar.DAY_OF_YEAR, 1);
        }
        
        // 把第一天加进去
        String firstDay = dateSimpleDateFormat.format(start);
        list.add(firstDay);
/*        if (dateBetweenObject.getMonthList().size() == 0 && dateBetweenObject.getYearList().size() == 0)
        {
        }*/
        
        dateBetweenObject.setDayEndCalendar(endCalendar);
        
        if (endCalendar.getTimeInMillis() == endDate.getTimeInMillis())
        {
            return false;
        }
        
        return true;
    }
    
/*    public static void main(String[] args)
        throws Exception
    {
        String st1 = "2017-06-13";
        String st2 = "2019-09-14";
        SimpleDateFormat dateSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date sd1 = dateSimpleDateFormat.parse(st1);
        Date sd2 = dateSimpleDateFormat.parse(st2);
        
        Calendar s1 = Calendar.getInstance();
        s1.setTime(sd1);
        Calendar s2 = Calendar.getInstance();
        s2.setTime(sd2);
        DateBetweenObject dateBetweenObject = process(s1.getTime(), s2.getTime());
        System.out.println(dateBetweenObject.getYearList());
        System.out.println(dateBetweenObject.getMonthList());
        System.out.println(dateBetweenObject.getDayList());
    }*/
}
